package net.sdd.school.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sdd.common.result.Result;
import net.sdd.school.entity.Course;
import net.sdd.school.entity.vo.CoursePublishVo;
import net.sdd.school.entity.vo.CourseVo;
import net.sdd.school.service.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Jet
 * @since 2022-08-22
 */
@RestController
@RequestMapping("/sddonline/course")
@Api(tags = "课程管理")
public class CourseController {
    @Autowired
    private ICourseService courseService;

    /**
     * 发布课程
     */
    @PostMapping
    @ApiOperation("发布课程")
    public Result addCourse(@RequestBody CourseVo courseVo) {
        String courseId = courseService.addCourse(courseVo);
        return Result.ok().data("courseId",courseId);
    }
    /**
     * 修改课程
     */
    @PutMapping
    @ApiOperation("修改课程")
    public Result updateCourse(@RequestBody CourseVo courseVo) {
        courseService.updateCourse(courseVo);
        return Result.ok();
    }

    /**
     * 根据课程id查询
     */
    @GetMapping("/{id}")
    @ApiOperation("根据课程id查询")
    public Result findCourseById(@PathVariable("id") String id) {
        CourseVo courseVo = courseService.findCourseById(id);
        return Result.ok().data("courseInfo",courseVo);
    }

    /**
     * 发布课程，课程确认查询
     */
    @GetMapping("/publish/{courseId}")
    @ApiOperation("课程确认查询")
    public Result findCoursePublish(@PathVariable("courseId")String courseId) {
        CoursePublishVo vo = courseService.findCoursePublish(courseId);
        return Result.ok().data("coursePublish",vo);
    }

    /**
     * 发布课程，修改课程状态
     */
    @PutMapping("/publish/{courseId}")
    @ApiOperation("发布课程，修改课程状态")
    public Result updateStatus(@PathVariable("courseId")String courseId) {
        // update ssd_course set status=? where id=?
        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status","Normal"); // 已发布
        updateWrapper.eq("id",courseId);
        // 执行修改
        courseService.update(updateWrapper);
        return Result.ok();
    }

    /**
     * 查询课程列表
     */
    @GetMapping
    @ApiOperation("查询课程列表")
    public Result findAll() {
        List<Course> list = courseService.list();
        return Result.ok().data("list",list);
    }
}

